package es.cea.tareas;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import es.cea.tareas.model.Tarea;

/**
 * Servlet implementation class ConfirmaEdicionTareaServlet
 */
public class ConfirmaEdicionTareaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConfirmaEdicionTareaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	Log logger=LogFactory.getLog(this.getClass());

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String parameterTitulo = request.getParameter("titulo");
			String parameterDescripcion = request.getParameter("descripcion");
			String parameterID = request.getParameter("idTarea");
			int idTarea=Integer.parseInt(parameterID);
			Object attribute = request.getSession().getServletContext().getAttribute("tareas");
			List<Tarea> tareas=(List<Tarea>) attribute;
			Tarea tarea=encuentraTarea(tareas, idTarea);

			tarea.titulo=parameterTitulo;
			tarea.descripcion=parameterDescripcion;
			response.getWriter().print("<h1>tarea correctamente editada!!</h1>" +
					"<a href='./ListaTareasServlet'>listado de tareas</a>" +
					"");
			

	}
	private Tarea encuentraTarea(List<Tarea> tareas, int idTarea) {
		for(Tarea t:tareas)
			if(t.id==idTarea) return t;
		throw new RuntimeException("la tarea con el id:"+idTarea+" NO EXISTE!!");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
